<?php
/**
 * Created by PhpStorm.
 * User: wuxiaofei
 * Date: 2018/5/25
 * Time: 9:42
 */
namespace V1;

class PlayList extends \ResourceClass
{
    public $isActive = true;
    public $remark = '我的竞猜';

    public function get(array $req, array $post, &$result){
        if (!isset($req["uid"]) || !$req['uid']) {
            return "uid is not null";
        }
        $checkRes = $this->autoCheck($req['uid']);
        if($checkRes['code'] !== 200){
            return $checkRes['message'];
        }
        $uid = $req['uid'];
        $page = $req['page']??1;

        //球队
        $redis_instance = \Util\Redis::getInstance();
        $player_list = $redis_instance->get('ziyan_player_list');
        $player_list = json_decode($player_list,true);

        //竞猜选项
        $setting_list = $redis_instance->get('ziyan_setting_list');
        $setting_list = json_decode($setting_list,true);

        //赛程信息
        $showing_list = $redis_instance->get('ziyan_showing_list');
        $showing_list = json_decode($showing_list,true);

        //获取我的竞猜
        $size = 10;
        $where = 'uid = '.$uid.' GROUP BY play_show_id';
        $resultData = \Biz\Play\Active::getListByShow($where,$page,$size);
        if($resultData){
            foreach ($resultData as $item){
                //竞猜结果
                $show_result = \Biz\Play\Result::getInfoBySid($item['play_show_id']);
                //我的竞猜
                $my_result = \Biz\Play\Active::getResultByUidAndSid($uid,$item['play_show_id']);
                //比赛信息
                $showing_info = $showing_list[$item['play_show_id']];

                //本次获得积分
                $score = 0;
                foreach ($show_result as $_key => $_val) {
                    if($my_result[$_key] == $_val && $_val !== 0){
                        if($_key == \Biz\Play\Settings::TYPE_A){
                            $score += \Util\Config::get('ziyan_score_a');
                        }elseif($_key == \Biz\Play\Settings::TYPE_B){
                            $score += \Util\Config::get('ziyan_score_b');
                        }elseif($_key == \Biz\Play\Settings::TYPE_C){
                            $score += \Util\Config::get('ziyan_score_c');
                        }
                    }
                }
                $_tmp = array(
                    'open_date' => date('Y-m-d',strtotime($showing_info['open_time'])),
                    'player_a' => $player_list[$showing_info['player_a']]['player_name'],
                    'player_b' => $player_list[$showing_info['player_b']]['player_name'],
                    'result_a' => ($show_result[\Biz\Play\Settings::TYPE_A] === $my_result[\Biz\Play\Settings::TYPE_A] && $show_result[\Biz\Play\Settings::TYPE_A] !== 0)?1:0,
                    'result_b' => ($show_result[\Biz\Play\Settings::TYPE_B] === $my_result[\Biz\Play\Settings::TYPE_B] && $show_result[\Biz\Play\Settings::TYPE_B] !== 0)?1:0,
                    'result_c' => ($show_result[\Biz\Play\Settings::TYPE_C] === $my_result[\Biz\Play\Settings::TYPE_C] && $show_result[\Biz\Play\Settings::TYPE_C] !== 0)?1:0,
                    'result_type_a' => $show_result[\Biz\Play\Settings::TYPE_A]?$setting_list[$show_result[\Biz\Play\Settings::TYPE_A]]['title']:'--',
                    'result_type_b' => $show_result[\Biz\Play\Settings::TYPE_B]?$setting_list[$show_result[\Biz\Play\Settings::TYPE_B]]['title']:'--',
                    'result_type_c' => $show_result[\Biz\Play\Settings::TYPE_C]?$setting_list[$show_result[\Biz\Play\Settings::TYPE_C]]['title']:'--',
                    'my_type_a' => $my_result[\Biz\Play\Settings::TYPE_A]?$setting_list[$my_result[\Biz\Play\Settings::TYPE_A]]['title']:'--',
                    'my_type_b' => $my_result[\Biz\Play\Settings::TYPE_B]?$setting_list[$my_result[\Biz\Play\Settings::TYPE_B]]['title']:'--',
                    'my_type_c' => $my_result[\Biz\Play\Settings::TYPE_C]?$setting_list[$my_result[\Biz\Play\Settings::TYPE_C]]['title']:'--',
                    'final_score' => ($showing_info['status'] == \Biz\Play\Showings::STATUS_FINISH)?$showing_info['score_a'].':'.$showing_info['score_b']:'',
                    'score' => $score
                );

                $result[] = $_tmp;
            }
        }
    }
}